import java.util.Arrays;
import java.util.Scanner;
public class race {
    public static void main1(String[] args) {
        //给定n个十六进制正整数，输出它们对应的八进制数。
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String [] str = new String[n];
        for (int i = 0; i < n; i++) {
            str[i] = sc.next();
        }
        sc.close();
        for (int i = 0; i < n; i++) {
            String strbinary = tobinary(str[i]);
            if(strbinary.length() % 3 == 1) strbinary = strbinary + "00";
            if(strbinary.length() % 3 == 2) strbinary = strbinary + "0";
            String strOctal = toOctal(strbinary);
            System.out.println(strOctal);
        }
    }
    private static String tobinary (String strHex ) {
        int hex_length = strHex.length();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < hex_length; i++) {
            switch (strHex.charAt(i)) {
                case '0': sb.append("0000"); break;
                case '1': sb.append("0001"); break;
                case '2': sb.append("0010"); break;
                case '3': sb.append("0011"); break;
                case '4': sb.append("0100"); break;
                case '5': sb.append("0101"); break;
                case '6': sb.append("0110"); break;
                case '7': sb.append("0111"); break;
                case '8': sb.append("1000"); break;
                case '9': sb.append("1001"); break;
                case 'A': sb.append("1010"); break;
                case 'B': sb.append("1011"); break;
                case 'C': sb.append("1100"); break;
                case 'D': sb.append("1101"); break;
                case 'E': sb.append("1110"); break;
                case 'F': sb.append("1111"); break;
                default: break;
            }
        }
        return sb.toString();
    }
    private static String toOctal (String strHex ) {
        StringBuffer sb2 = new StringBuffer();
        int k;
        if(strHex.substring(0,2).equals("000")) {
            k = 3;
        }else {
            k = 0;
        }
        for (int i = k; i < strHex.length()-2; i+= 3) {
            switch (strHex.substring(i,i+3)) {
                case "001": sb2.append('1'); break;
                case "010": sb2.append('2'); break;
                case "011": sb2.append('3'); break;
                case "100": sb2.append('4'); break;
                case "101": sb2.append('5'); break;
                case "110": sb2.append('6'); break;
                case "111": sb2.append('7'); break;
                default: break;
            }
        }
        return sb2.toString();
    }
    //十六进制转十进制
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();


    }
}
